package com.jxzn.locust.controller.admin;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jxzn.locust.dto.ResultDTO;
import com.jxzn.locust.model.Manager;
import com.jxzn.locust.model.Role;
import com.jxzn.locust.service.AbstractService;
import com.jxzn.locust.service.ManagerService;
import com.jxzn.locust.service.RoleService;
import com.jxzn.locust.support.Auth;
import com.jxzn.locust.support.Module;
import com.jxzn.locust.support.OperateAction;
import com.jxzn.locust.utils.ModelUtils;

@Controller
@RequestMapping("/bg/manager")
@Module("人员管理")
public class ManagerController extends NamedEntityController<Manager> {

    @Autowired
    ManagerService managerService;
    @Autowired
    RoleService roleService;

    @Override
    public AbstractService<Manager> getService() {
        return managerService;
    }

    @Override
    public String getFolder() {
        return "manager";
    }

    /**
     * 去列表页
     * 
     * @param model
     * @param query
     * @return
     */
    @RequestMapping("/list")
    @Auth(action = OperateAction.VIEW)
    @Override
    public String findAll(HttpServletRequest request, Model model, Manager query) {
        List<Manager> dataList = getService().findAll(query);

        Map<String, Role> roleMap = ModelUtils.list2MapWithStringKey(roleService.findAll());
        for (Manager m : dataList) {
            Role role = roleMap.get(String.valueOf(m.getRoleId()));
            if (null != role) {
                m.setRoleName(role.getName());
            }
        }
        model.addAttribute("dataList", dataList);
        return "/bg/" + getFolder() + "/list";
    }

    /**
     * 去编辑页面
     * 
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/edit")
    @Auth(action = OperateAction.VIEW)
    @Override
    public String edit(HttpServletRequest request, Model model, Manager query, @RequestParam(value = "id", required = false, defaultValue = "0") int id) {
        String action = "添加";
        if (id > 0) {
            Optional<Manager> optional = getService().findById(id);
            if (optional.isPresent()) {
                model.addAttribute("target", optional.get());
                action = "编辑";
            }
        }

        Role rq = new Role();
        rq.setDeleted(Boolean.FALSE);
        model.addAttribute("roleList", roleService.findAll(rq));
        model.addAttribute("action", action);
        return "/bg/" + getFolder() + "/edit";
    }

    /**
     * 添加/保存
     * 
     * @param query
     * @param model
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    @Auth(action = OperateAction.EDIT)
    public ResultDTO<?> saveOrUpdate(Manager manager, Model model) {
        if (null == manager.getId()) {
            Manager query = new Manager();
            query.setAccount(manager.getAccount());
            Optional<Manager> optional = getService().getOne(query);
            if (optional.isPresent()) {
                return ResultDTO.param("已经存在相同的登录账号，请修改");
            }
            manager.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes()));
        } else {
            manager.setUpdateTime(Calendar.getInstance().getTime());
            if (manager.getModifyPassword()) {
                manager.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes()));
            } else {
                Optional<Manager> optional = getService().findById(manager.getId());
                manager.setPassword(optional.get().getPassword());
            }
        }
        getService().save(manager);
        return ResultDTO.success();
    }
}
